// zh_cjdianc/index_new/pay.js
var a = getApp(), util = require("../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shop:{
      logo:'http://192.168.10.25/attachment/images/2/2018/09/ik75775t67h5LZU5TJJZu6I7D57U17.png',
      name:'饭戒（软件园店）',
      address:'武汉市洪山区软件园B区2栋',
      tel:'027-88888888'
    },
    user:[],
    price:0,
    price1:0,
    popErrorMsg: '',
    order_id:'',

  },
  goFreeShake:function(){
    var n = this;
    var store_id = n.data.shop.id;
    var price1 = n.data.price1;
    var order_id = n.data.order_id;
    var exp = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
    if (!exp.test(price1)||price1 ==0) {
      n.setData(
        { popErrorMsg: "请输入正确的金额" }
      )
      n.ohShitfadeOut(); return;
    }
    a.util.request({
      url: "entry/wxapp/xfpay",
      cachetime: "0",
      data: { order_id: order_id, total_fee: price1 },
      success: function (t) {
        console.log(t.data)
        if (t.data.code == 0) {
          // var c = t.data.data.return;
          // wx.requestPayment(
          //   {
          //     'timeStamp': c.timeStamp,
          //     'nonceStr': c.nonceStr,
          //     'package': c.package,
          //     'signType': c.signType,
          //     'paySign': c.paySign,
          //     'success': function (res) {
          //     },
          //     'fail': function (res) { },
          //     'complete': function (res) { }
          //   })
          wx.reLaunch({
              url: '../free/freeShake?store_id=' + store_id,
              success: function(res) {},
              fail: function(res) {},
              complete: function(res) {},
            })
        } else {
          n.setData(
            { popErrorMsg: t.data.message }
          )
          n.ohShitfadeOut(); return;
        }
      }
    });
  },
  tapprice:function(e){
    var n = this;
    var score = n.data.user.total_score;
    var exp = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
    var price = e.detail.value;
    var price1 = e.detail.value;
    n.setData({
      price1: price1
    });
    if (!exp.test(price)){
      n.setData(
        { popErrorMsg: "请输入正确的金额" }
      )
      n.ohShitfadeOut(); return;
    }
    price = Number(price);
    if (score < price){
      price = price - score;
    }else{
      price = 0;
    }
    n.setData({
      price: price
    });
  },
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var n = this;
    var user_id = wx.getStorageSync('users').id;
    a.setNavigationBarColor(this);
    var order_id = options.order_id;
    a.util.request({
      url: "entry/wxapp/yyygetinfo",
      cachetime: "0",
      data: { order_id: order_id},
      success: function (t) {
        n.setData({
          shop: t.data.data.store,
          user: t.data.data.user,
          order_id: t.data.data.order_id,
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})